﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using WebSysCore.JSons;
using WebSysCore;
using WebSysCore.Helpers;
using HSchSchedule.Utils;

namespace HSchSchedule.AjaxPages
{
    public partial class AjaxFCST : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod(true)]
        public static string GetGroup()
        {
            int cusId;

            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Group> lstGr = DataBase.GetAllGroup(schId);
                    List<JGroup> lstJGr = new List<JGroup>();
                    foreach (Group g in lstGr)
                    {
                        lstJGr.Add(AssignHelp.GroupToJGroup(g));
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJGr);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetClassOfGroup(int groupId)
        {
            int cusId;

            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Class> lstClass = DataBase.GetClassOfGroup(groupId);
                    List<JClass> lstJClass = new List<JClass>();
                    foreach (Class cl in lstClass)
                    {
                        lstJClass.Add(AssignHelp.ClassToJClass(cl));
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJClass);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string GetGVCN(int classId)
        {
            int cusId;

            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    Teacher gvcn = DataBase.GetGVCNOfClass(classId);
                    List<Subject> lstSub = DataBase.GetSubjectOfTeacher(gvcn.TeacherID, classId);
                    JTeacher jGvcn = AssignHelp.TeacherTpJTeacher(gvcn);
                    List<JSubject> lstJSub = new List<JSubject>();
                    foreach (Subject s in lstSub)
                    {
                        lstJSub.Add(AssignHelp.SubToJSub(s));
                    }
                    JTeacherInfo jt = new JTeacherInfo()
                    {
                        T = jGvcn,
                        LstSub = lstJSub
                    };
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(jt);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string GetSubTeach(int classId, int gvcnSubId)
        {
            int cusId;

            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    Teacher gvcn = DataBase.GetGVCNOfClass(classId);
                    List<Subject> lstSub = DataBase.GetSubjectOfTeacher(gvcn.TeacherID, classId);
                    JTeacher jGvcn = AssignHelp.TeacherTpJTeacher(gvcn);
                    List<JSubject> lstJSub = new List<JSubject>();
                    foreach (Subject s in lstSub)
                    {
                        lstJSub.Add(AssignHelp.SubToJSub(s));
                    }
                    JTeacherInfo jt = new JTeacherInfo()
                    {
                        T = jGvcn,
                        LstSub = lstJSub
                    };
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(jt);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string GetSubjectInfo(int classId, int gvcnSubId)
        {
            int cusId;

            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Subject> lstSub = DataBase.GetSubjectOfClass(classId);
                    List<JSubjectInfo> lstJSubIn = new List<JSubjectInfo>();
                    foreach (Subject s in lstSub)
                    {
                        if (s.SubjectID == gvcnSubId || s.NotNeedTeacher > 0)
                        {
                            //lstSub.Remove(s);
                            //break;
                        }
                        else
                        {
                            JSubjectInfo jsi = new JSubjectInfo();
                            jsi.Sub = AssignHelp.SubToJSub(s);
                            jsi.LstTeach = new List<JTeacher>();
                            List<Teacher> lstTeach = DataBase.GetTeacherOfSubject(s.SubjectID);
                            foreach (Teacher t in lstTeach)
                            {
                                jsi.LstTeach.Add(AssignHelp.TeacherTpJTeacher(t));
                            }
                            lstJSubIn.Add(jsi);
                        }
                    }


                    //List<Subject> lstSub=DataBase.GetSubjectOfGroup(
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJSubIn);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetFixedInfo(int classId, int subId)
        {
            int cusId;

            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    Teacher t = DataBase.GetTeachFixOfClass(classId, subId);
                    string t2;
                    if (t != null)
                    {
                        t2 = t.TeacherID.ToString();
                    }
                    else
                    {
                        t2 = "0";
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(t2);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string GetTeacherFixInfo(int classId,int teacherId)
        {
            int cusId;

            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    Subject s = DataBase.GetFixInfoOfTeacher(classId, teacherId);
                    string t2;
                    if (s != null)
                    {
                        t2 = s.SubjectID.ToString();
                    }
                    else
                    {
                        t2 = "0";
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(t2);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string SaveFixInfo(List<JFixTeacherClassSubject> lstJFix)
        {
            int cusId;

            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    bool ok = true;
                    List<FixTeacherClassSubject> lstFix = new List<FixTeacherClassSubject>();
                    foreach (JFixTeacherClassSubject jf in lstJFix)
                    {
                        ok = ValidateHelp.ValidateFCST(jf);
                        if (ok)
                        {
                            FixTeacherClassSubject f = AssignHelp.JFixTCSToFixTCS(jf);
                            f.ScheduleInListId = schId;
                            lstFix.Add(f);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (ok)
                    {
                        //Chen vo CSDL roi bao SUCCESS
                        DataBase.SaveFCSTInfo(lstFix);
                        return GUIConstants.SUCCESS_RESULT;
                    }
                    else
                    {
                        return GUIConstants.FAIL_RESULT;
                    }
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }

    }
}
